var height = 400;
height = $(window).height() -200;

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'business/tbanner/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: 'banner名称', name: 'bannerName', index: 'banner_name', width: 80 },
			{ label: '链接地址', name: 'image', index: 'image', width: 80 },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,formatter:function (cellValue) {
				return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(cellValue))
			}},
			{ label: '状态', name: 'status', index: 'status', width: 80,formatter:function (cellValue) {
				return harCodeName(cellValue,"","",switchJson,"");
			} },
			{ label: '最近操作时间', name: 'updateTime', index: 'update_time', width: 80 ,formatter:function (cellValue) {
				return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(cellValue))
			}},
			{ label: '最近操作者', name: 'operator', index: 'operator', width: 80 },
			{label:'操作',formatter:function (cellValue,options,row) {

				var  template = "";

				if(row.status == 0){

					template += '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.update(\''+row.id+'\',\'1\')">启动</a>';

					template += '<a style="margin-right: 15px;color:#337ab7" href="javascript:void(0)" onclick="vm.add(null,\''+row.id+'\')">编辑</a>';

					template += '<a style="margin-right: 15px;color:#337ab7" href="javascript:void(0)" onclick="vm.del(null,\''+row.id+'\')">删除</a>';

				}else{
					template += '<a style="margin-right: 15px;color:#337ab7" href="javascript:void(0)" onclick="vm.update(\''+row.id+'\',\'0\')">禁用</a>';
				}

				return template;


			}}
        ],
		viewrecords: true,
        height: height,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

	//文件上传
	layui.use('upload', function(){
		layui.upload({
			url:'/admin/upload',
			success:function (r) {
				if(r.code  == '0'){
					vm.msg = "上传成功";
					vm.tBanner.image = r.url;
				}
			}
		});
	});

});





var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tBanner:{
			image:"",
			url:"",
			bannerName:""
		},
		msg:''
	}
	,
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(event,rowId){
			vm.title = "新增";
			if(typeof(rowId) != 'undefined'){
				vm.getInfo(rowId);
				vm.title = "编辑";
			}

			layer.open({
				type:1,
				skin: 'layui-layer-lan',
				title: vm.title,
				area: ['500px', '350px'],
				offset:"20px",
				scrollbar:true,
				closeBtn:0,
				content: jQuery("#add"),
				btn: ['确定', '取消'],
				success: function (layer) {
					$(layer[0].childNodes[3].childNodes[0]).css({"background": "#337ab7", "color": "#fff"});
					$(layer[0].childNodes[3].childNodes[1]).css({
						"background": "#fff",
						"color": "rgb(51, 51, 51)",
						"border": "1px solid #C9C5C5"
					});
				},
				btn1: function(index){
					if(vm.tBanner.bannerName.length <= 0){
						alert("banner名称必填");
						return;
					}

					if(vm.tBanner.bannerName.length >= 15){
						alert("banner名称不超过15个字");
						return;
					}

					console.log(vm.tBanner.url)
					if(vm.tBanner.image == null || vm.tBanner.image.length == 0 ){
						alert("请先上传图片");
						return;
					}


					vm.saveOrUpdate("",index);
				},
				btn2: function(index, layero){
					vm.clear();
					vm.msg = "";
				}
			});

		},
		update: function (id,status) {
            vm.getInfo(id);
			vm.tBanner.status = status;
			vm.saveOrUpdate();
		},
		saveOrUpdate: function (event,layerIndex) {
			var url = vm.tBanner.id == null ? "business/tbanner/save" : "business/tbanner/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.tBanner),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							layer.close(layerIndex);
							vm.clear();
							vm.msg = "";
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event,rowId) {
			confirm('确定要删除？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "business/tbanner/delete/"+rowId,
                    contentType: "application/json",
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			ajax(baseURL + "business/tbanner/info/"+id,{},"get",false,false).then(function (r) {

				vm.tBanner = r.tBanner;

			})
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		clear:function () {
			vm.tBanner = {
				image:"",
				url:"",
				bannerName:""
			}
		}
	}
});